package com.wenjie.controller.sys;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.json.JSONUtil;
import com.wenjie.original.entity.SysMenu;
import com.wenjie.original.entity.vo.SysMenu.MoveVo;
import com.wenjie.original.entity.vo.SysMenu.SysMenuAddVo;
import com.wenjie.original.entity.vo.SysMenu.SysMenuUpVo;
import com.wenjie.original.entity.vo.SysMenu.SysMenuVo;
import com.wenjie.original.service.ISysMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api("菜单栏处理")
@RestController
@RequestMapping("/api/menu")
public class SysMenuController {

    public static final String STRUCTURE = "structure";
    public static final String LIST = "list";

    @Autowired
    private ISysMenuService sysMenuService;

    @ApiOperation("初始化")
    @PostMapping("/init")
    public Map<String, Object> init() {
        Map<String, Object> restmap = new HashMap<>();

        // TODO 筛选出来 当前用户有那些菜单的权限
        List<SysMenu> list = sysMenuService.screenPermission();

        String jsonStr = JSONUtil.toJsonStr(list);
        System.out.println(jsonStr);

        restmap.put(STRUCTURE, list); // 将数据转换为树形结构
        restmap.put(LIST, sysMenuService.filterC(list)); // 筛选菜单栏
        return restmap;
    }

    @ApiOperation("查询接口")
    @PostMapping("/list")
    public List<SysMenu> list(@RequestBody @Validated SysMenuVo.SelVo selVo) {

        // TODO 筛选出来 当前用户有那些菜单的权限

        List<SysMenu> list = sysMenuService.list();
        return list; // 将数据转换为树形结构
    }

    @ApiOperation("查询接口")
    @PostMapping("/ListAll")
    public List<ISysMenuService.Menu> ListAll() {
        List<SysMenu> list = sysMenuService.list();
        return sysMenuService.treeStructure(list); // 将数据转换为树形结构
    }


    @ApiOperation("移动数据")
    @PostMapping("/move")
    public void move(@RequestBody @Validated MoveVo moveVo) {
        sysMenuService.move(moveVo);
    }


    @ApiOperation("删除接口")
    @PostMapping("/del")
    public void del(@RequestBody @Validated Long id) {
        sysMenuService.delById(id);
    }


    @ApiOperation("添加目录接口")
    @PostMapping("/addM")
    public void addM(@RequestBody @Validated SysMenuAddVo.AddMVo addVo) {
        addVo.setMenuType(SysMenu.MENU_TYPE.argM);

        Long snowflakeNextId = IdUtil.getSnowflakeNextId();
        addVo.setSysMenuId(snowflakeNextId);
        addVo.setMenuOrder(snowflakeNextId);

        sysMenuService.save(addVo);
    }

    @ApiOperation("添加菜单接口")
    @PostMapping("/addC")
    public void addC(@RequestBody @Validated SysMenuAddVo.AddCVo addVo) {
        addVo.setMenuType(SysMenu.MENU_TYPE.argC);

        Long snowflakeNextId = IdUtil.getSnowflakeNextId();
        addVo.setSysMenuId(snowflakeNextId);
        addVo.setMenuOrder(snowflakeNextId);

        sysMenuService.save(addVo);
    }

    @ApiOperation("修改目录接口")
    @PostMapping("/updateM")
    public void updateM(@RequestBody @Validated SysMenuUpVo.UpdateMVo updateMVo) {
        SysMenu bean = BeanUtil.toBean(updateMVo, SysMenu.class);
        sysMenuService.updateById(bean);
    }

    @ApiOperation("修改菜单接口")
    @PostMapping("/updateC")
    public void updateC(@RequestBody @Validated SysMenuUpVo.UpdateCVo updateCVo) {
        SysMenu bean = BeanUtil.toBean(updateCVo, SysMenu.class);
        sysMenuService.updateById(bean);
    }

}
